<template>
  <div class="default-main ba-table-box">
    <TableHeader
      :buttons="[
        'refresh',
        'add',
        'edit',
        'remove',
        'comSearch',
        'columnDisplay',
        'upload',
        'export',
      ]"
      :quick-search-placeholder="placeholder"
      @action="baTable.onTableHeaderAction"
    />
    <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
    <Table ref="tableRef" @action="baTable.onTableAction" />
    <!-- 表单 -->
    <PopupForm
      v-if="
        (baTable.form.operate !== 'upload' && baTable.form.operate) ||
        baTable.form.operate === 'edit'
      "
    />
    <!-- 上传excel文件 -->
    <Upload v-if="baTable.form.operate && baTable.form.operate === 'upload'" />
  </div>
</template>
<script lang="ts" setup>
import {
  ref,
  provide,
  onMounted,
  markRaw,
  reactive,
  onBeforeUnmount,
} from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff, getDictData } from "@/utils/common"; // 导入表格api方法生成器
import { BomProcess } from "@/api/backend/index";
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import PopupForm from "./popupForm.vue";
import Upload from "./components/import.vue";
import { useRoute } from "vue-router";
import { useNavTabs } from "@/store/navTabs";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useDictionary } from "@/store/backend/tool/dictionary";
import { useFactoryEquipment } from "@/store/backend/FactoryM/equipmentUnit";
import { useFactoryStation } from "@/store/backend/FactoryM/stationInfo";
import { useBomM } from "@/store/backend/BomM/bomM";
import { useBomRoute } from "@/store/backend/BomM/bomRoute";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { useBomProcess } from "@/store/backend/BomM/bomProcess";
import TagVue from "./components/tag.vue";

// import { _post } from "@/utils/axios";
const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const navTabs = useNavTabs();
const dictionary = useDictionary();
const equipUnit = useFactoryEquipment();
const station = useFactoryStation();
const bom = useBomM();
const bomR = useBomRoute();
const bomP = useBomProcess();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能 默认10个
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + BomProcess)
      ? (navTabs as any).pageSizeMap.get(route.path + BomProcess)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(BomProcess),
  {
    pk: "id", //区域ID
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("code"),
        label: "工序号",
        prop: "code",
        align: "left",
      },
      {
        show: filedShowOff("name"),
        label: "工序名称",
        prop: "name",
        align: "left",
      },

      {
        show: filedShowOff("type"),
        label: "工序类型",
        prop: "type",
        align: "left",
        render: "customRender",
        customRender: markRaw(TagVue),
        operator: false,
      },
      {
        show: filedShowOff("prepareTime"),
        label: "单件额定准备时间",
        prop: "prepareTime",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("productTime"),
        label: "单件额定加工时间",
        prop: "productTime",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("afterTime"),
        label: "单件额定结束时间",
        prop: "afterTime",
        align: "left",
        operator: false,
      },
      {
        show: operatorShowOff([
          route.path + "/edit",
          route.path + "/add",
          route.path + "/remove",
        ]),
        label: "操作",
        align: "center",
        width: "130",
        operator: false,
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      pageNum: 1,
      pageSize: pageSizeKeep(),
    },
  },
  {
    defaultItems: {
      type: 1, //默认机加工
      prepareTime: "0", //默认工序额定准备时间
      productTime: "0", //默认单件额定加工时间
      afterTime: "0", //默认单件额定收尾时间
    },
  }
);

//接收工作中心树传递过来的数据
const filetrStation = (row: any, node: any) => {
  console.log("接收bom树传递过来的数据");

  if (node.checkedKeys.length == 0) {
    //表示取消选择直接调用刷新表单方法
    // baTable.comSearch.form = Object.assign(baTable.comSearch.form, {
    //   regions: region.regionIds + "",
    // });
    // baTable.onTableHeaderAction("refresh", {});
    bom.$patch((state: any) => {
      state.bomInfo = {};
    });
  } else {
    baTable.comSearch.form.materialCode = row.code;
    baTable.comSearch.form.materialType = row.type;
    if (node.halfCheckedNodes.length > 0) {
      baTable.comSearch.form.parentCode = node.halfCheckedNodes[0].code;
    } else {
      baTable.comSearch.form.parentCode = "";
    }
    baTable.getList(BomProcess);
    bom.$patch((state: any) => {
      state.bomInfo = row;
    });
  }
};

//获取字典
const getMenuStatus = () => {
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["processType"])
  ) {
    dictionary.getDictInfo("sys_process_type").then((res: any) => {
      dictionary.moreTags["processType"] = getDictData(res);
      // plan.options["state"] = res;
      console.log("获取工序类型的字典", dictionary.moreTags);
    });
  }
};
getMenuStatus();

onMounted(() => {
  const { proxy } = useCurrentInstance();

  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(BomProcess);

  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {}
  );
});

onBeforeUnmount(() => {
  bom.$patch((state: any) => {
    state.bomInfo = {};
  });
});
// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("dictionary", dictionary);
provide("equipUnit", equipUnit);
provide("station", station);
provide("bom", bom);
provide("bomR", bomR);
provide("bomP", bomP);
</script>
<style lang="scss" scoped>
.default-main {
  margin: 0px !important;
}
</style>
